package sandbox;

import java.net.DatagramSocket;

/**
 * This code should allow for seamless GMS 
 * replacement.
 * @author psyomn
 */
public class SocketHolder implements Runnable 
{
	private DatagramSocket mSock; 
	private boolean mIsOccupying = false; 
	
	public SocketHolder(int port)
	{	
		while(!mIsOccupying)
		{
			try
	    {
				/* try and occupy a socket */
				Thread.currentThread().sleep(500);
				mSock = new DatagramSocket(port); 
				mIsOccupying = true;
				System.out.println("Opened socket: " + port);
	    } catch (Exception e)
	    {
	    	System.out.println("Someone is listening at that socket; sleeping.");
	    }
		}
	}

	@Override
  public void run()
  {
	  while(true)
	  {
	  	// do something ... 
	  }
  }
	
	public static void main(String[] args)
	{
		SocketHolder sh = new SocketHolder(50000);
		sh.run();
	}
}
